package com.ajmd.onlineexam.service;


import com.ajmd.onlineexam.entity.DepartmentPO;
import com.ajmd.onlineexam.entity.OrganizationPO;
import com.ajmd.onlineexam.entity.TeamPO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.mapper.DepartmentMapper;
import com.ajmd.onlineexam.mapper.TeamMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author ajmd
 * @since 2023/5/1 23:55
 */
@Service
public class TeamService {
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private DepartmentMapper departmentMapper;

    public int addOrUpdate(TeamPO teamPO){
        if(ObjectUtils.isNotEmpty(teamPO.getId())){
            return teamMapper.updateById(teamPO);
        }
        //上级部门子小组数量+1
        LambdaUpdateWrapper<DepartmentPO> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
        lambdaQueryWrapper.eq(DepartmentPO::getId, teamPO.getDepartmentId()).setSql("team_num = team_num + 1");
        departmentMapper.update(null, lambdaQueryWrapper);
        teamPO.setId(UUID.randomUUID().toString());
        return teamMapper.insert(teamPO);
    }

    public Page<TeamPO> getPage(Integer currentPage, Integer pageSize, SearchParam searchParams){
        LambdaQueryWrapper<TeamPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(searchParams.getSearchValue())){
            lambdaQueryWrapper.like(TeamPO::getTeamName,searchParams.getSearchValue());
        }
        Page<TeamPO> page = new Page<>(currentPage,pageSize);
        return teamMapper.selectPage(page,lambdaQueryWrapper);
    }

    public List<TeamPO> getList(List<String> ids){
        LambdaQueryWrapper<TeamPO> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtils.isNotEmpty(ids)){
            queryWrapper.in(TeamPO::getId,ids);
        }
        return teamMapper.selectList(queryWrapper);
    }


    public TeamPO getOne(String id){
        return teamMapper.selectById(id);
    }

//    public int update(TeamPO teamPO){
//        return teamMapper.updateById(teamPO);
//    }
    public int deleteByDepartmentId(String departmentId){
        LambdaQueryWrapper<TeamPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TeamPO::getDepartmentId,departmentId);
        return teamMapper.delete(lambdaQueryWrapper);
    }

    @Transactional
    public int delete(String id){
        TeamPO teamPO = teamMapper.selectById(id);
        if(ObjectUtils.isEmpty(teamPO)){
            return 0;
        }
        //上级部门子小组数量-1
        LambdaUpdateWrapper<DepartmentPO> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
        lambdaQueryWrapper.eq(DepartmentPO::getId, teamPO.getDepartmentId()).setSql("team_num = team_num - 1");
        departmentMapper.update(null, lambdaQueryWrapper);
        return teamMapper.deleteById(teamPO);
    }
}
